package com.xiaobai.customer.exception;


import com.xiaobai.customer.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.nio.file.AccessDeniedException;

/**
 * 异常处理器
 */
@RestControllerAdvice
@ResponseBody
@Slf4j
public class ExceptionResultHandler {

    @ExceptionHandler(Exception.class)
    public ResponseResult<String> handleException(Exception e) {
        if (e instanceof TipException) {
            log.error(e.getMessage());
            Integer code = ((TipException) e).getCode();

            return ResponseResult.fail(code, e.getMessage());
        } else {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            return ResponseResult.fail(e.getMessage());
        }
    }

    @ExceptionHandler(BusinessFeignException.class)
    public ResponseResult<String> handleBusinessFeignException(BusinessFeignException e) {
        log.error(e.getMessage(), e);
        return ResponseResult.fail(e.getErrorCode(), e.getErrorMsg());
    }

}
